//
//  WKSelfieView.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/18.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKSelfieView.h"

@interface WKSelfieView ()
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,copy)void(^block)(void);
@end

@implementation WKSelfieView

+ (void)showWKSelfieViewTakeCamera:(void(^)(void))takeCamera{
    WKSelfieView *view = [[WKSelfieView alloc]initWithFrame:ScreenBounds];
    [view show];
    view.block = takeCamera;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        [self setup];
    }
    return self;
}

- (void)setup{
    

    CGFloat content_height = 316;
    CGFloat content_width = 292;

    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, content_width, content_height)];
    contentView.backgroundColor = [UIColor clearColor];
    self.contentView = contentView;
    
    UIImageView *titleimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, content_width, content_height)];
    titleimgv.backgroundColor = [UIColor clearColor];
    [titleimgv wh_topCornerWithRadius:5];
    titleimgv.layer.masksToBounds = YES;
    titleimgv.image = ImageNamed(@"wk_faceselfie");
    titleimgv.userInteractionEnabled = YES;

    [contentView addSubview:titleimgv];
    
    
//    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(10, imgHeight, contitle_width, contentHeight+44)];
//    titleView.backgroundColor = [UIColor whiteColor];
//    [titleView wh_bottomCornerWithRadius:5];
//    titleimgv.layer.masksToBounds = YES;
//    [contentView addSubview:titleView];
    
    UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 174, content_width, 18)];
    titleLb.text = @"找到相似好友";
    titleLb.font = PFSC_SemiboldFont(FLoatChange(19));
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.textColor = UIColorFromHex(0x3C3C3C);
    [titleimgv addSubview:titleLb];
    
    UILabel *deLb1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 212, content_width, 20)];
    deLb1.text = @"匹配相似好友";
    deLb1.font = PFSC_SemiboldFont(15);
    deLb1.textAlignment = NSTextAlignmentCenter;
    deLb1.textColor = UIColorFromHex(0x3C3C3C);
    [titleimgv addSubview:deLb1];
    
    UILabel *deLb2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 242, content_width, 20)];
    deLb2.text = @"查询如何变美";
    deLb2.font = PFSC_SemiboldFont(15);
    deLb2.textAlignment = NSTextAlignmentCenter;
    deLb2.textColor = UIColorFromHex(0x3C3C3C);
    [titleimgv addSubview:deLb2];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, content_height-44, content_width, 1)];
    line.backgroundColor = UIColorFromHex(0xF8F8F8);
    [titleimgv addSubview:line];
    
    UILabel *deLb3 = [[UILabel alloc]initWithFrame:CGRectMake(0, content_height-43, content_width, 43)];
    deLb3.text = @"立即自拍";
    deLb3.font = PFSC_SemiboldFont(16);
    deLb3.textAlignment = NSTextAlignmentCenter;
    deLb3.textColor = UIColorFromHex(0xFF3657);
    [titleimgv addSubview:deLb3];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, content_height-44, content_width, 44);
    [titleimgv addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.contentView = contentView;
    
    
}

- (void)btnClick:(UIButton*)sender{
    [self hidden];
    if (self.block) {
        self.block();
    }
}

- (void)show{
    
    [self addSubview:_contentView];
    _contentView.center = self.center;
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.backgroundColor = [UIColor clearColor];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [keyWindow addSubview:self];
    NSArray *view_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    NSArray *view_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    [keyWindow addConstraints:view_H];
    [keyWindow addConstraints:view_V];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:tap];
    
    _contentView.alpha=0;
    [UIView animateWithDuration:0.1f animations:^{
        _contentView.alpha=1;
    }];
    
}

- (void)hidden{
    
    [UIView animateWithDuration:0.2f animations:^{
        _contentView.alpha=0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
}


@end
